<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
/**
 * Created by PhpStorm.
 * User: XiaoBingBy
 * Date: 2017/10/27
 * Time: 9:09
 */
class ClazzCourseTeacher extends Controller
{

    public function index() {
        // 查询班级
        $clazz = DB::name('clazz')->select();

        // 查询课程
        $course = DB::name('course')->select();

        // 查询教师
        $emp = DB::name('emp')->select();

        $this->assign(['clazz'=>$clazz, 'course'=>$course, 'emp'=>$emp]);
        return $this->fetch();
    }

    public function getList($pageNumber = 1, $pageSize = 5, $searchText = '') {
        // 连接分页查询
        $result = DB::field('t1.*, oes_clazz.`name` AS clazzName,
                        oes_clazz.`code` AS clazzCode,
                        oes_course.`name` AS courseName,
                        oes_course.`code` AS courseCode,
                        oes_emp.`name` AS empName,
                        oes_emp.work_number')
            ->table(['oes_clazzcourseteacher'=>'t1'])
            ->join('oes_clazz', 'oes_clazz.id = t1.clazz_id')
            ->join('oes_course', 'oes_course.id = t1.course_id')
            ->join('oes_emp', 'oes_emp.id = t1.teacher_id')
            ->paginate($pageSize, false, ["page"=>$pageNumber]);
        return json($result);
    }

    public function addClazzCourseTeacher(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['clazz_id', 'course_id', 'teacher_id']);
        // 添加数据
        $result = DB::name('clazzcourseteacher')->insert($data);
        // 返回数据
        if ($result >= 1) {
            return json(['success'=>true, "msg"=>'添加成功', $result]);
        } else {
            return json(['success'=>false, "msg"=>'添加失败']);
        }
    }

    public function updateClazzCourseTeacher(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['id', 'clazz_id', 'course_id', 'teacher_id']);
        // 更新数据
        $result = DB::name('clazzcourseteacher')->update($data);
        // 返回数据
        if ($result >= 1) {
            return json(['success'=>true, "msg"=>'修改成功', $result]);
        } else {
            return json(['success'=>false, "msg"=>'修改失败']);
        }
    }

    public function delete($id) {
        $result = DB::name('clazzcourseteacher')->delete($id);
        if ($result >= 1) {
            return json(['success'=>true, "msg"=>'删除成功']);
        } else {
            return json(['success'=>false, "msg"=>'删除用户不存在']);
        }
    }

}